package com.seeyoui.kensite.framework.system.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class BeanUtils {
    /**
     * 根据列名插入数据
     * 
     * @param cls 实体对象
     * @param columnName 列名
     * @param value 值
     * @throws SecurityException
     * @throws NoSuchFieldException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @SuppressWarnings({"unchecked","rawtypes", "deprecation"})
    public static void setColumnValue(Object obj, String columnName, String value) throws NoSuchFieldException, SecurityException,
            NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Class cls = obj.getClass();
        Field field = cls.getDeclaredField(columnName);
        Method method = cls.getDeclaredMethod("set" + firstToUpperCase(columnName), field.getType());
        switch (field.getType().toString()) {
        case "class java.lang.String":
            method.invoke(obj, value);
            break;
        case "class java.lang.Integer":
            method.invoke(obj, Integer.parseInt(value));
            break;
        case "class java.lang.Double":
            method.invoke(obj, Double.valueOf(value));
            break;
        case "class java.lang.Boolean":
            method.invoke(obj, Boolean.valueOf(value));
            break;
        case "class java.util.Date":
            method.invoke(obj, Date.parse(value));
            break;
        case "class java.lang.Short":
            method.invoke(obj, Short.valueOf(value));
            break;
        default:
            break;
        }
    }

    /**
     * 对象数据转换 被注入对象中如果某个字段有值则数据源对象的数据不会注入该列
     * 
     * @param currentBean
     *            被注入的对象
     * @param sourcesBean
     *            数据源对象
     * @return currentBean
     * @throws Exception
     */
    @SuppressWarnings({"unchecked","rawtypes", "deprecation"})
    public static Object beanTransmit(Object currentBean, Object sourcesBean) throws Exception {
        Class sourcesCls = sourcesBean.getClass();
        Class currentCls = currentBean.getClass();
        if (sourcesCls.isInstance(currentBean)) {
            Field[] sourcesFields = sourcesCls.getDeclaredFields();
            for (Field field : sourcesFields) {
                Field currentField = currentCls.getDeclaredField(field.getName());
                if (currentField != null) {
                    Method currentMethod = currentCls.getDeclaredMethod("get" + firstToUpperCase(field.getName()));
                    Object currentFieldValue = currentMethod.invoke(currentBean);
                    Method sourcesGetMethod = sourcesCls.getDeclaredMethod("get" + firstToUpperCase(field.getName()));
                    Method sourcesSetMethod = sourcesCls.getDeclaredMethod("set" + firstToUpperCase(field.getName()), field.getType());
                    if (currentFieldValue == null || currentFieldValue.toString().equals("0")) {
                        switch (field.getType().toString()) {
                        case "class java.lang.String":
                            sourcesSetMethod.invoke(currentBean, sourcesGetMethod.invoke(sourcesBean).toString());
                            break;
                        case "class java.lang.Integer":
                            sourcesSetMethod.invoke(currentBean, Integer.parseInt(sourcesGetMethod.invoke(sourcesBean).toString()));
                            break;
                        case "int":
                            sourcesSetMethod.invoke(currentBean, Integer.parseInt(sourcesGetMethod.invoke(sourcesBean).toString()));
                            break;
                        case "class java.lang.Double":
                            sourcesSetMethod.invoke(currentBean, Double.valueOf(sourcesGetMethod.invoke(sourcesBean).toString()));
                            break;
                        case "class java.lang.Boolean":
                            sourcesSetMethod.invoke(currentBean, Boolean.valueOf(sourcesGetMethod.invoke(sourcesBean).toString()));
                            break;
                        case "class java.util.Date":
                            sourcesSetMethod.invoke(currentBean, Date.parse(sourcesGetMethod.invoke(sourcesBean).toString()));
                            break;
                        case "class java.lang.Short":
                            sourcesSetMethod.invoke(currentBean, Short.valueOf(sourcesGetMethod.invoke(sourcesBean).toString()));
                            break;
                        default:
                            break;
                        }
                    }
                } else {
                    throw new Exception("匹配对象中不包含\b\b" + field.getName() + "\b\b列");
                }
            }
            return currentBean;
        } else {
            throw new Exception("当前对象不匹配");
        }
    }

    /**
     * 首字母大写
     * 
     * @param str
     * @return
     */
    private static String firstToUpperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    public static void main(String[] args) {
        try {
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
